import { defineConfig } from "vite";
import dts from "vite-plugin-dts";
import path from "path";

export default defineConfig({
  server: {
    host: "0.0.0.0",
    port: 5678,
    open: "/examples/basic/host.html",
  },

  build: {
    lib: {
      entry: "src/index.ts",
      name: "FastBridge",
      formats: ["es", "umd", "iife"],
      fileName: (format) => {
        switch (format) {
          case "es":
            return "fast-bridge.es.js";
          case "umd":
            return "fast-bridge.umd.js";
          case "iife":
            return "fast-bridge.min.js";
          default:
            return "fast-bridge.js";
        }
      },
    },
    rollupOptions: {
      external: [],
      output: {
        globals: {},
        // 为IIFE格式添加全局变量名
        extend: true,
        // 确保IIFE格式正确导出
        exports: "named",
        // 自定义IIFE格式，将FastBridge类直接挂载到全局
        format: "iife",
        name: "FastBridge",
        // 自定义输出，确保FastBridge类直接可用
        intro: `
          if (typeof window !== 'undefined') {
            window.FastBridge = window.FastBridge || {};
          }
        `,
        outro: `
          if (typeof window !== 'undefined' && window.FastBridge && window.FastBridge.FastBridge) {
            window.FastBridge = window.FastBridge.FastBridge;
          }
        `,
      },
    },
    // 生成sourcemap
    sourcemap: true,
  },
  plugins: [dts()],

  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src"),
    },
    extensions: [".ts", ".js", ".json"],
  },
});
